# Maintainer: Alexey Pavlov <Alexpux@gmail.com>
# Contributor: Ray Donnelly <mingw.android@gmail.com>

_realname=colorama
pkgbase=mingw-w64-python-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-python2-${_realname}" "${MINGW_PACKAGE_PREFIX}-python3-${_realname}")
pkgver=0.4.1
pkgrel=4
pkgdesc="Python API for cross-platform colored terminal text (mingw-w64)"
arch=('any')
license=('BSD')
url="https://pypi.python.org/pypi/colorama"
makedepends=("${MINGW_PACKAGE_PREFIX}-python2-setuptools"
             "${MINGW_PACKAGE_PREFIX}-python3-setuptools")
checkdepends=("${MINGW_PACKAGE_PREFIX}-python2-mock"
              "${MINGW_PACKAGE_PREFIX}-python2-mock")
options=('staticlibs')
source=("${_realname}-${pkgver}.tar.gz::https://github.com/tartley/colorama/archive/${pkgver}.tar.gz"
        df2ca5e9237f46d1e48b6115b87f37db6abfa908.patch)
sha256sums=('6b2e2bb7bd96813fcafe5598886ce4f9f741450677e1ee090a119c600f04de9c'
            '75feae882be925c472b59908bb21ac5906474836e093aef7add2a55568858ab1')

apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying $_patch"
    patch -Nbp1 -i "${srcdir}/$_patch"
  done
}

prepare() {
  cd "${srcdir}"
  pushd "${_realname}-${pkgver}"
    apply_patch_with_msg df2ca5e9237f46d1e48b6115b87f37db6abfa908.patch
  popd
  for builddir in python{2,3}-build-${CARCH}; do
    rm -rf ${builddir} | true
    cp -r "${_realname}-${pkgver}" "${builddir}"
  done
}

build() {
  for pver in {2,3}; do  
    msg "Python ${pver} build for ${CARCH}"  
    cd "${srcdir}/python${pver}-build-${CARCH}"
    ${MINGW_PREFIX}/bin/python${pver} setup.py build
  done  
}

check() {
  for pver in {2,3}; do
    msg "Python ${pver} test for ${CARCH}"
    cd "${srcdir}/python${pver}-build-${CARCH}"
    ${MINGW_PREFIX}/bin/python${pver} -m unittest discover -p *_test.py || warning "Tests failed"
  done
}


package_python2-colorama() {
  depends=("${MINGW_PACKAGE_PREFIX}-python2")

  cd "${srcdir}/python2-build-${CARCH}"
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
   ${MINGW_PREFIX}/bin/python2 setup.py install --prefix=${MINGW_PREFIX} \
    --root="${pkgdir}" --optimize=1 --skip-build

  install -Dm644 LICENSE.txt "${pkgdir}${MINGW_PREFIX}/share/licenses/python2-${_realname}/COPYING"
}

package_python3-colorama() {
  depends=("${MINGW_PACKAGE_PREFIX}-python3")

  cd "${srcdir}/python3-build-${CARCH}"
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
   ${MINGW_PREFIX}/bin/python3 setup.py install --prefix=${MINGW_PREFIX} \
    --root="${pkgdir}" --optimize=1 --skip-build

  install -Dm644 LICENSE.txt "${pkgdir}${MINGW_PREFIX}/share/licenses/python3-${_realname}/COPYING"
}

package_mingw-w64-i686-python2-colorama() {
  package_python2-colorama
}

package_mingw-w64-i686-python3-colorama() {
  package_python3-colorama
}

package_mingw-w64-x86_64-python2-colorama() {
  package_python2-colorama
}

package_mingw-w64-x86_64-python3-colorama() {
  package_python3-colorama
}
